package com.mineoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mineoj.common.BaseResponse;
import com.mineoj.common.DeleteRequest;
import com.mineoj.common.ErrorCode;
import com.mineoj.common.ResultUtils;
import com.mineoj.exception.BusinessException;
import com.mineoj.exception.ThrowUtils;
import com.mineoj.model.dto.solutioncomment.SolutionCommentAddRequest;
import com.mineoj.model.dto.solutioncomment.SolutionCommentQueryRequest;
import com.mineoj.model.entity.Question;
import com.mineoj.model.entity.SolutionComment;
import com.mineoj.model.entity.User;
import com.mineoj.model.vo.SolutionCommentVO;
import com.mineoj.service.SolutionCommentService;
import com.mineoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目提交接口
 *
 
 */
@RestController
@RequestMapping("/solution_comment")
@Slf4j
public class SolutionCommentController {

    @Resource
    private SolutionCommentService solutionCommentService;

    @Resource
    private UserService userService;

    /**
     * 提交评论
     *
     * @param solutionCommentAddRequest
     * @param request
     * @return 提交记录的 id
     */
    @PostMapping("/add")
    public BaseResponse<Long> doSolutionComment(@RequestBody SolutionCommentAddRequest solutionCommentAddRequest,
            HttpServletRequest request) {
        if (solutionCommentAddRequest == null || solutionCommentAddRequest.getSolutionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交评论
        final User loginUser = userService.getLoginUser(request);
        long solutionCommentId = solutionCommentService.doSolutionComment(solutionCommentAddRequest, loginUser);
        return ResultUtils.success(solutionCommentId);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        SolutionComment solutionComment = solutionCommentService.getById(id);
        ThrowUtils.throwIf(solutionComment == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!solutionComment.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = solutionCommentService.deleteSolutionComment(id, user);
        return ResultUtils.success(b);
    }


    /**
     * 分页获取评论列表
     *
     * @param solutionCommentQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<SolutionCommentVO>> listSolutionCommentByPage(@RequestBody SolutionCommentQueryRequest solutionCommentQueryRequest,
                                                                         HttpServletRequest request) {
        long current = solutionCommentQueryRequest.getCurrent();
        long size = solutionCommentQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<SolutionComment> solutionCommentPage = solutionCommentService.page(new Page<>(current, size));
        return ResultUtils.success(solutionCommentService.getSolutionCommentVOPage(solutionCommentPage, request));
    }
}
